$(function(){
    var ajaxFill = new Z.AjaxFill({
	url: Z.url('/backend/message/records',true),
	templates: {
	    list: $('#list-template'),
	    none: '<tr><td colspan="6" style="text-align:center;">记录为空</td></tr>',
	    loading: '<tr><td colspan="6" style="text-align:center;"><img src="../assets/img/backend/loading.gif"/></td></tr>'
	},
	data : {
	    pageSize : 20
	},
	containers: {
	    list: $('table.records tbody'),
	    pageLinks: $('table.records tfoot td:eq(1)')
	}
    });
    
    $('a.delete').live('click',function(){
	var id = Z.parseInt($(this).parent().siblings().first().find('input:checkbox').val());
	Z.confirm({
	    content: '确定删除吗?',
	    onConfirm: function(){
		Z.ajax({
		    url: Z.url('/backend/message/delete/'+id,true),
		    success: function(){
			ajaxFill.reload();
		    }
		});
	    }
	});
	return false;
    });
    
    $('a.select-all').click(function(){
	$('table.records tbody td input:checkbox:not(:checked)').trigger('click');
    });
    
    $('a.select-reverse').click(function(){
	$('table.records tbody td input:checkbox').trigger('click');
    });
    
    $('a.delete-batch').click(function(){
	var ids = [];
	$('table.records tbody td input:checkbox:checked').each(function(){
	    ids.push(Z.parseInt($(this).val()));
	});
	Z.confirm({
	    content: '确定删除吗?',
	    onConfirm: function(){
		Z.ajax({
		    url: Z.url('/backend/message/delete_batch',true),
		    data: {
			ids:ids
		    },
		    success: function(){
			ajaxFill.reload();
		    }
		});
	    }
	});
    });
    
    $('a.add').click(function(){
	var data  = {
	    action:'insert',
	    name:'',
	    content : ''
	};
	Z.form({
	    width: 600,
	    content : $('#form-template').tmpl(data),
	    success : function(){
		ajaxFill.reload();
	    }
	});
    });
    
    $('a.edit').live('click',function(){
	var id = Z.parseInt($(this).parent().siblings().first().find('input:checkbox').val());
	Z.ajax({
	    url: Z.url('/backend/message/find/'+id,true),
	    success: function(data){
		data.action = 'update';
		Z.form({
		    width: 600,
		    content : $('#form-template').tmpl(data),
		    success : function(){
			ajaxFill.reload();
		    }
		});
	    }
	});
	return false;
    });

    ajaxFill.request();
});